class Animal {
    public String name;
    public int  age;

    /*public Animal() {

    }*/

    /*static {
        System.out.println("Animal::static{}");
    }

    {
        System.out.println("Animal::{}");
    }
*/
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println("Animal(String , int )");
    }

    public  void eat() {
        System.out.println(name + "吃饭！");
    }

    public void function() {
        System.out.println("Animal::function()!");
    }

}

class Dog extends Animal{
    //傻狗  是狗的属性
    public boolean silly;
    //public String name = "hello";

   /* public Dog() {
        super();
    }*/

    /*static {
        System.out.println("Dog::static{}");
    }

    {
        System.out.println("Dog::{}");
    }*/

    public Dog(String name,int age,boolean silly) {
        //1. 先帮助父类部分初始化 必须放到第一行
        super(name,age);
        //super(); 不写 默认都会有
        this.silly = silly;
        System.out.println("Dog(String ,int ,boolean )");
    }

    public void houseGuard () {
        System.out.println(super.name+"正在看家护院！");
        //实例内部当中 才会包含外部类的this
        //System.out.println(Animal.this.name+"正在看家护院！");
        //eat();
        staticFunc();
    }

    public static void staticFunc() {
        //System.out.println(super.name);
    }

    @Override
    public void eat() {
        System.out.println(name+"正在吃狗粮！");
    }


}
class Cat extends Animal{


    /*static {
        System.out.println("Cat::static{}");
    }

    {
        System.out.println("Cat::{}");
    }*/


    public Cat() {
        super("咪咪",10);
        //this("咪咪",10);
        System.out.println("Cat()");
    }

    public Cat(String name,int age) {
        super(name,age);
    }

    //不加特有的属性了
    public void catchMouse() {
        System.out.println(name + "抓老鼠！");
    }

    @Override
    public void eat() {
        System.out.println(name+" 吃猫粮 ！");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal animal2 = new Dog("hello",10,false);
        //animal2.catchMouse();
        // animal2 是不是引用了 Cat这个对象
        if(animal2 instanceof Cat) {
            Cat cat = (Cat) animal2;
            cat.catchMouse();
        }
    }

    public static void main4(String[] args) {
        Animal animal2 = new Cat("hello",10);
        //animal2.catchMouse(); error
        Cat cat = (Cat)animal2;
        cat.catchMouse();
    }

    //2. 方法的传参过程当中 也可以发生向上转型
    public static void function2(Animal animal) {
        animal.eat();
    }

    public static Animal function3() {
        return new Cat();
    }

    public static void main3(String[] args) {
        Dog dog = new Dog("hello",10,false);
        function2(dog);

        Cat cat = new Cat("haha",7);
        function2(cat);


    }
    public static void main2(String[] args) {
        Dog dog = new Dog("hello",10,false);
        //animal这个引用 指向了Dog对象
        Animal animal = dog;
        //animal.silly = false; 不能访问 原因是 没有这个属性。 此时的animal 只能调用自己特有的属性。
        animal.eat();

        //1. 直接赋值
        Animal animal2 = new Dog("hello",10,false);

        Animal animal3 = new Cat();

    }

}
